package com.project.server.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.server.common.api.Result;
import com.project.server.dto.ConsultationStatsDTO;
import com.project.server.entity.Consultation;
import com.project.server.entity.DoctorAppointment;
import com.project.server.entity.HealthRecord;
import com.project.server.entity.SysUser;
import com.project.server.service.DoctorService;
import com.project.server.service.ConsultationService;
import com.project.server.service.SysUserService;
import com.project.server.utils.JwtUtil;
import com.project.server.vo.doctor.DoctorVO;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/doctor")
public class DoctorController extends BaseController{

    @Autowired
    private DoctorService doctorService;

    @Autowired
    private SysUserService sysUserService;


    @Autowired
    private ConsultationService consultationService;

    @GetMapping("/consultation/stats")
    public Result<ConsultationStatsDTO> getConsultationStats(HttpServletRequest request) {
        Long doctorId = getCurrentUserId(request);
        return Result.success(consultationService.getDoctorStats(doctorId));
    }

    @GetMapping("/appointments/today")
    public Result<List<Map<String, Object>>> getTodayAppointments(HttpServletRequest request) {
        Long doctorId = JwtUtil.getUserIdFromRequest(request);
        return Result.success(doctorService.getTodayAppointments(doctorId));
    }


    @GetMapping("/patient/{patientId}/records")
    public Result<List<HealthRecord>> getPatientRecords(@PathVariable Long patientId) {
        return Result.success(doctorService.getPatientHealthRecords(patientId));
    }

    @GetMapping("/appointments")
    public Result<IPage<DoctorAppointment>> getAppointmentList(
        @RequestParam Long doctorId,
        @RequestParam(required = false) Integer status,
        @RequestParam(defaultValue = "1") Integer current,
        @RequestParam(defaultValue = "10") Integer size
    ) {
        return Result.success(doctorService.getAppointmentList(doctorId, status, current, size));
    }

    @PutMapping("/appointment/{id}/status")
    public Result<Boolean> updateAppointmentStatus(
        @PathVariable Long id,
        @RequestParam Integer status
    ) {
        return Result.success(doctorService.updateAppointmentStatus(id, status));
    }

    @PutMapping("/appointment/{id}/record")
    public Result<Void> updateAppointmentRecord(
        @PathVariable Long id,
        @RequestBody Map<String, String> record
    ) {
        doctorService.updateAppointmentRecord(
            id,
            record.get("diagnosis"),
            record.get("prescription")
        );
        return Result.success();
    }


    @GetMapping("/list")
    public Result<IPage<SysUser>> list(
        @RequestParam(defaultValue = "1") Integer current,
        @RequestParam(defaultValue = "10") Integer size,
        @RequestParam(required = false) String name,
        @RequestParam(required = false) Long deptId
    ) {
        return Result.success(sysUserService.page(current, size, name, deptId));
    }

    @GetMapping("/getAllDoctor")
    public Result<IPage<SysUser>> getAllDoctor(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) Long deptId
    ) {
        return Result.success(sysUserService.page(current, size, name, deptId));
    }

    @PostMapping
    public Result<Boolean> save(@RequestBody DoctorVO doctorVO) {
        return Result.success(doctorService.saveDoctor(doctorVO));
    }

    @PutMapping
    public Result<Boolean> update(@RequestBody DoctorVO doctorVO) {
        return Result.success(doctorService.updateDoctor(doctorVO));
    }

    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Long id) {
        return Result.success(doctorService.removeById(id));
    }

    @GetMapping("/info")
    public Result<SysUser> getDoctorInfo(@RequestParam Long doctorId) {
        return Result.success(doctorService.getDoctorInfo(doctorId));
    }

    @GetMapping("/consultations")
    public Result<Page<Consultation>> getConsultations(
            @RequestParam(required = false) Integer status,
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            HttpServletRequest request) {
        Long doctorId = getCurrentUserId(request);
        return Result.success(consultationService.getDoctorConsultations(doctorId, status, current, size));
    }

    @PutMapping("/service/status")
    public Result<Void> updateServiceStatus(@RequestParam Boolean online, HttpServletRequest request) {
        Long doctorId = getCurrentUserId(request);
        consultationService.updateDoctorOnlineStatus(doctorId, online);
        return Result.success();
    }
}
